package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.RejectDao;
import com.aaa.book_mall.entity.Reject;
import com.aaa.book_mall.util.JDBCUtil;

import java.util.List;
import java.util.Map;

/**
 * fileName:RejectDaoImpl
 * description:
 * author:付亚星
 * createTime:2021/1/30 19:37
 * version:1.0.0
 */

public class RejectDaoImpl implements RejectDao {


    @Override
    public List<Map<String, Object>> getAllReject(Reject reject) {
        StringBuilder sql = new StringBuilder("select * from t_reject where status='2'");
        //          根据条件拼接sql语句
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql.toString(),(reject.getPage() - 1) * reject.getLimit(), reject.getLimit());
        return maps;
    }

    /**
     * 查询数据总条数，不论状态
     * @return
     */
    @Override
    public int queryCountAll() {
        String sql="select count(*) from t_reject";
        List<List> lists = JDBCUtil.queryList(sql);
        int count=0;
        if(lists.size()>0){
            count=Integer.parseInt(lists.get(0).get(0).toString());
        }
        return count;
    }

    /**
     * 查询状态为2的数量
     * @return
     */
    @Override
    public int queryNoCountAll() {
        String sql="select count(*) from t_reject where status='2'";
        List<List> lists = JDBCUtil.queryList(sql);
        int count=0;
        if(lists.size()>0){
            count=Integer.parseInt(lists.get(0).get(0).toString());
        }
        return count;
    }

    /**
     * 查询对应订单
     * @param audit_number
     * @return
     */
    @Override
    public List<Map<String, Object>> showInfoReject(String audit_number) {
        System.out.println(audit_number);
        String sql = "SELECT td.audit_number,tb.bookname,tb.price,td.bookcount,tb.count,tb.publisher from t_detail td inner JOIN t_book tb on tb.bookid=td.bookid INNER JOIN t_audit ta ON  ta.audit_number=td.audit_number where td.bookid in (select bookid from t_detail where audit_number=?) and ta.audit_number=?";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql, audit_number, audit_number);
        return maps;

    }

    @Override
    public int insertReject(Reject reject) {
        String sql = "insert into t_reject values(null,?,?,?,?,?,?,?)";
        int update = JDBCUtil.update(sql, reject.getAudit_number(), reject.getStaffid(), reject.getStaffname(), reject.getBookcount(), reject.getBookprice(), reject.getCreatedate(), reject.getStatus());

        return update;
    }

    /**
     * 修改驳回订单审核状态
     * @param audit_number
     * @param status
     * @return
     */
    @Override
    public int updateStatus(String audit_number, String status) {
        System.out.println("进入修改驳回状态");
        String sql="update t_reject set status=? where audit_number=?";
        int i = JDBCUtil.update(sql,status, audit_number);
        return i;
    }
}
